package com.scrm.server.wx.cp.service.impl;

import com.scrm.common.dto.BatchDTO;
import lombok.extern.slf4j.Slf4j;
import com.scrm.server.wx.cp.entity.BrSopDetail;
import com.scrm.server.wx.cp.mapper.BrSopDetailMapper;
import com.scrm.server.wx.cp.service.IBrSopDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scrm.server.wx.cp.dto.BrSopDetailPageDTO;
import com.scrm.server.wx.cp.dto.BrSopDetailSaveDTO;
import com.scrm.server.wx.cp.dto.BrSopDetailUpdateDTO;

import com.scrm.server.wx.cp.dto.BrSopDetailQueryDTO;
import com.scrm.server.wx.cp.vo.BrSopDetailVO;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import com.scrm.common.util.UUID;
import com.scrm.common.exception.BaseException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.stream.Collectors;

/**
 * 群sop-规则执行详情表 服务实现类
 * @author ouyang
 * @since 2022-04-17
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class BrSopDetailServiceImpl extends ServiceImpl<BrSopDetailMapper, BrSopDetail> implements IBrSopDetailService {

    @Override
    public IPage<BrSopDetailVO> pageList(BrSopDetailPageDTO dto){
        LambdaQueryWrapper<BrSopDetail> wrapper = new QueryWrapper<BrSopDetail>()
        .lambda().eq(BrSopDetail::getExtCorpId, dto.getExtCorpId());
        IPage<BrSopDetail> page = page(new Page<>(dto.getPageNum(), dto.getPageSize()),wrapper);
        return page.convert(this::translation);
    }


    @Override
    public List<BrSopDetailVO> queryList(BrSopDetailQueryDTO dto){
        LambdaQueryWrapper<BrSopDetail> wrapper = new QueryWrapper<BrSopDetail>()
        .lambda().eq(BrSopDetail::getExtCorpId, dto.getExtCorpId());
        return Optional.ofNullable(list(wrapper)).orElse(new ArrayList<>()).stream().map(this::translation).collect(Collectors.toList());
    }


    @Override
    public BrSopDetailVO findById(String id){
        return translation(checkExists(id));
    }


    @Override
    public BrSopDetail save(BrSopDetailSaveDTO dto){

        //封装数据
        BrSopDetail brSopDetail = new BrSopDetail();
        BeanUtils.copyProperties(dto,brSopDetail);
        brSopDetail.setId(UUID.get32UUID());

        //入库
        save(brSopDetail);

        return brSopDetail;
    }


    @Override
    public BrSopDetail update(BrSopDetailUpdateDTO dto){

        //校验参数
        BrSopDetail old = checkExists(dto.getId());

        //封装数据
        BrSopDetail brSopDetail = new BrSopDetail();
        BeanUtils.copyProperties(dto, brSopDetail);
//        brSopDetail.setCreateTime(old.getCreateTime())
//        .setCreator(old.getCreator());


        //TODO

        //入库
        updateById(brSopDetail);

        return brSopDetail;
    }


    @Override
    public void delete(String id){

        //校验参数
        BrSopDetail brSopDetail = checkExists(id);

        //删除
        removeById(id);

    }


    @Override
    public void batchDelete(BatchDTO<String> dto){

        //校验参数
        List<BrSopDetail> brSopDetailList = new ArrayList<>();
        Optional.ofNullable(dto.getIds()).orElse(new ArrayList<>()).forEach(id -> brSopDetailList.add(checkExists(id)));

        //删除
        removeByIds(dto.getIds());
    }


    /**
     * 翻译
     * @param brSopDetail 实体
     * @return BrSopDetailVO 结果集
     * @author ouyang
     * @date 2022-04-17
     */
    private BrSopDetailVO translation(BrSopDetail brSopDetail){
        BrSopDetailVO vo = new BrSopDetailVO();
        BeanUtils.copyProperties(brSopDetail, vo);
        return vo;
    }


    @Override
    public BrSopDetail checkExists(String id){
        if (StringUtils.isBlank(id)) {
            return null;
        }
        BrSopDetail byId = getById(id);
        if (byId == null) {
            throw new BaseException("群sop-规则执行详情表不存在");
        }
        return byId;
    }
}
